package com.example.learning_store;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button save;
    Button load;
    Button bt_change_activity;
    Button bt_change_activity2;
    TextView show_value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /**
         *
         */
        save=findViewById(R.id.save);
        load=findViewById(R.id.load);
        bt_change_activity=findViewById(R.id.bt_change_activity);
        bt_change_activity2=findViewById(R.id.bt_change_activity2);
        show_value=findViewById(R.id.show_value);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences("data_one", Context.MODE_PRIVATE);
                sp.edit().putString("lxh","father").apply();//apply才会将东西写入到xml文件中
            }
        });
        load.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp=getSharedPreferences("data_one",Context.MODE_PRIVATE);
                String value = sp.getString("lxh", "default");
                show_value.setText(value);
            }
        });
        bt_change_activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MainActivity2.class);
                startActivity(intent);
                MainActivity.this.finish();
            }
        });
        bt_change_activity2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MainActivity3.class);
                startActivity(intent);
                MainActivity.this.finish();
            }
        });
    }

}